#!/usr/bin/env python
# encoding: utf-8
'''
@author: zhaowenpeng
@contact: winston@peipeiyun.com
@software: garner
@file: __init__.py.py
@time: 2020/7/17 3:43 下午
@desc:
'''

from core.my_log import logger
from peewee import __exception_wrapper__,InterfaceError
from playhouse.pool import PooledMySQLDatabase
from peewee import OperationalError
import traceback

class RetryOperationalError(object):
    def execute_sql(self, sql, params=None, commit=True):
        try:
            cursor = super(RetryOperationalError, self).execute_sql(
                sql, params, commit)
        except InterfaceError:
            try:
                logger.error(traceback.format_exc())
                self.connect(reuse_if_open=True)
                cursor = super(RetryOperationalError, self).execute_sql(
                        sql, params, commit)
            except OperationalError:
                logger.error(traceback.format_exc())
                if not self.is_closed():
                    self.close()
                with __exception_wrapper__:
                    cursor = self.cursor()
                    cursor.execute(sql, params or ())
                    if commit and not self.in_transaction():
                        self.commit()
        except OperationalError:
            logger.error(traceback.format_exc())
            if not self.is_closed():
                self.close()
            with __exception_wrapper__:
                cursor = self.cursor()
                cursor.execute(sql, params or ())
                if commit and not self.in_transaction():
                    self.commit()
        return cursor

class RetryMySQLDatabase(RetryOperationalError,PooledMySQLDatabase):
    pass

import random
import datetime
class DBUtil(object):
    def get_id_by_show_id(self, showId):
        '''
        根据showid获取真实id值
        :param showId: type string 自动增长列id值
        :return:
        '''
        i = showId[len(showId) - 1]
        j = showId[0:len(showId) - 1]
        k = (int(j) - int(i) - 10000) / int(i)
        return int(k)

    def set_show_id_by_id(self, id):
        '''
        设置showid
        :param id: 真实id
        :return:
        '''
        a = random.randint(1, 9)
        b = a * int(id) + 10000 + a
        j = str(b) + str(a)
        return j

    def to_dict(self,keys):
        data={}
        for key in keys:
            value = getattr(self,key)
            if isinstance(value,datetime.datetime):
                data[key]=value.strftime("%Y-%m-%d %H:%M:%S")
            else:
                data[key] = value
        return data

    @classmethod
    def get_id_by_show_id(cls, showId):
        '''
        根据showid获取真实id值
        :param showId: type string 自动增长列id值
        :return:
        '''
        i = showId[len(showId) - 1]
        j = showId[0:len(showId) - 1]
        k = int((int(j) - int(i) - 10000) / int(i))
        return k
    @classmethod
    def set_show_id_by_id(self, id):
        '''
        设置showid
        :param id: 真实id
        :return:
        '''
        a = random.randint(1, 9)
        b = a * int(id) + 10000 + a
        j = str(b) + str(a)
        return j




